
//获取游客列表
var userList; //游客列表
function getUserInformation() {
    if($("#underprivileged:has(div)").length!=0)
    {
        return void(0);
    }
    $.ajax({
        //几个参数需要注意一下
        type: "get",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers: {'authorization': header},
        url: "http://120.79.183.142/api/user/list",//url
        success: function (result) {
            // console.log(result);//打印服务端返回的数据(调试用)
            if (result["code"] == 200) {
                userList = result["data"];
                showUserList();
            }
            else{


            }

        },
        error: function (r) {
        }
    });
}

//显示游客列表
function showUserList(){

    var num = userList.length;//表格所有行数(所有记录数)
    console.log(userList)
    //遍历显示数据实现分页
    // $("#underprivileged").empty();
    for(var i=1;i<(num+1);i++){
            var irow = userList[i-1];
            var id = irow["id"];
            var real_name = irow["real_name"];
            var borrow_id = irow["borrow_id"];
            var state = irow["state"];
            switch (state) {
                case "0":state="<p style='color: green' id='userState"+id+"'>正常</p>";break;
                case "1":state="<p style='color: #e5e525;' id='userState"+id+"'>超时</p>";break;
                case "2":state="<p style='color: red;' id='userState"+id+"'>封禁</p>";break;
            }
            var over_timescale_times = irow["over_timescale_times"];
            var not_pay_fine_times = irow["not_pay_fine_times"];
            var borrow_times = irow["borrow_times"];

            var str = "<tr>"
                + "<td  style=\"font-size: medium\">" + id + "</td>"
                + "<td  style=\"font-size: medium\">" + borrow_id + "</td>"
                + "<td  style=\"font-size: medium\">" + "<span>" + real_name + "</span>" + "</td>"
                + "<td style=\"font-size: medium\">" + borrow_times + "</td>"
                + "<td  style=\"font-size: medium\">" + over_timescale_times + "</td>"
                + "<td style=\"font-size: medium\">" + not_pay_fine_times + "</td>"
                + "<td style=\"font-size: medium\"  id='uid"+id+"'>" + state + "</td>"
                + "<td >"
                + "<button class=\"btn btn-success btn-xs\" onclick=\"returnBooks1(this)\" style='margin-right: 10px'>还书</button>"
                + "<button class=\"btn btn-danger btn-xs\" data-toggle=\"modal\" data-target=\"#changeChar\" onClick=\"changeState1(this)\">修改状态</button>"
                + "</td>" + "</tr>";

            $("#underprivileged").append(str);

    }

    $("#underprivileged").DataTable({
        destroy: true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    });


}

//更改用户状态
var userId;
function changeState1(button){
    userId = button.parentNode.parentNode.children[0].innerHTML;
}
function changeState2() {

    var changeStateData = $("#changeStateForm").serialize();
    changeStateData +="&uid="+userId;
    $.ajax({
        //几个参数需要注意一下
        type: "post",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers: {'authorization': header},
        url: "http://120.79.183.142/api/user/change/state",//url
        data: changeStateData,
        success: function (result) {
            // console.log(result);//打印服务端返回的数据(调试用)
            if (result["code"] == 200) {
                document.getElementById("cancleChangeState").click();
                var State=$("#sOrd").val()
                switch (State) {
                    case "0":State="<p style='color: green' id='userState"+userId+"'>正常</p>";break;
                    case "2":State="<p style='color: red;' id='userState"+userId+"'>封禁</p>";break;
                }

                console.log(State)
                // document.getElementById("uid"+userId).remove();
                // $("#uid"+userId).append(State);

                document.getElementById("uid"+userId).innerHTML=State
                // $("#uid"+userId)
                // document.getElementById("underprivileged").children[userId-1].children[6].innerHTML=$("#sOrd").val();
            }
            else{
                console.log(result);
            }

        },
        error: function (r) {
        }
    });
}

//未归还书籍显示
var waitingToReturn;
function returnBooks1(button) {
    userId = button.parentNode.parentNode.children[0].innerHTML;
    if($("#returnBooks:has(div)").length!=0){
        $("#returnBooks").empty();
    }
    $(".meun-item.returnBooks").click();

    $.ajax({
        //几个参数需要注意一下
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers: {'authorization': header},
        url: "http://120.79.183.142/api/user/not/return",//url
        data:{"uid":userId},
        success: function (result) {
            // console.log(result);//打印服务端返回的数据(调试用)
            if (result["code"] == 200) {
                waitingToReturn = result["data"];
                returnBooks2();
            }
            else{
            }

        },
        error: function (r) {
        }
    
});
}
function returnBooks2(){

    var num = waitingToReturn.length;//表格所有行数(所有记录数)
    for(var i=0;i<num;i++){
        var irow = waitingToReturn[i];

        var borrow_id = irow["borrow_id"];
        var state = irow["state"];
        switch (state) {
            case 0: state="<p style='color: green;margin-top: 20px'>正常</p>";break;
            case 1: state="<p style='color: #e5e525;margin-top: 20px'>超时</p>";break;
            case 2: state="<p style='color: #805055;margin-top: 20px'>超时归还</p>";break;
            case 3: state="<p style='color: gray;margin-top: 20px'>正常归还</p>";break;
        }

        var name = irow["name"];
        var created_at = irow["created_at"];
        var timescale = irow["timescale"];
        var fine = irow["fine"];
            var str = "<div class=\"row \">"
                + "<div class=\"col-xs-1\">" + borrow_id + "</div>"
                + "<div class=\"col-xs-2\">" + "<span>"+name +"<span>"+ "</div>"
                + "<div class=\"col-xs-2\">" + created_at  + "</div>"
                + "<div class=\"col-xs-2\">" + timescale + "</div>"
                + "<div class=\"col-xs-1\">" + state + "</div>"
                + "<div class=\"col-xs-2\">" + fine + "元</div>"
                + "<div class=\"col-xs-2 checkbox checkbox-info\" style='margin-top: 25px'>"
                + "<input name=\"returnCheck\" class=\"styled\" type=\"checkbox\" >"
                + "还书"
                + "</div>" + "</div>";

            $("#returnBooks").append(str);
    }

    var str2 = "<button class=\"btn btn-success btn-xs\" onclick=\"returnBooks3()\">还书</button>";
     document.getElementById("barcon2").innerHTML = str2;
}

var borrow_ids = new Array();
function returnBooks3() {
    borrow_ids.splice(0,borrow_ids.length);
    var checkLength=document.getElementsByName("returnCheck");
    for(var clength=0;clength<checkLength.length;clength++ ){
        if(checkLength[clength].checked){
            borrow_ids.push(checkLength[clength].parentNode.parentNode.children[0].innerHTML);
            console.log(borrow_ids);
        }
    }

    $.ajax({
        //几个参数需要注意一下
        type: "POST",//方法类型
        dataType: "json",//预期服务器返回的数据类型'
        headers: {'authorization': header},
        url: "http://120.79.183.142/api/book/return",//url
        data:{"borrow_id":borrow_ids},
        success: function (result) {
            console.log(result);//打印服务端返回的数据(调试用)
            if (result["code"] == 200) {
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);
                for(var clength=0;clength<checkLength.length;clength++ ){
                    if(checkLength[clength].checked){
                        checkLength[clength].parentNode.parentNode.remove();
                        clength--;
                    }
                }
            }
            else if(result["code"] == 400008){
                console.log(result);
                console.log(result["data"]);
                window.location.href=("http://www.inf.ink./api/alipay/index?price="+result["data"]);
            }

        },
        error: function (r) {
            console.log(r);
        }

    });
    console.log(borrow_ids);


}
